package util.util;

import java.io.*;

public class FileSerialization {
    /**
     * 序列化对象
     * @param path 文件地址
     * @param obj  对象
     */
    public static void fileserialization(String path,Object obj){
        FileOutputStream fileOutputStream=null;
        ObjectOutputStream objectOutputStream=null;
        try {
            fileOutputStream=new FileOutputStream(path);
            objectOutputStream=new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(obj);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 反序列化对象
     * @param path  文件地址
     * @return
     */
    public static Object serializationfile(String path){
        FileInputStream fileInputStream=null;
        ObjectInputStream objectInputStream=null;
        Object obj=null;
        try {
            fileInputStream=new FileInputStream(path);
            objectInputStream=new ObjectInputStream(fileInputStream);
            obj=objectInputStream.readObject();
            return obj;
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } finally{
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
